package com.scrm.server.wx.cp.web;

import com.scrm.common.exception.BaseException;
import com.scrm.common.log.annotation.Log;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.scrm.api.wx.cp.dto.FissionPosterParamDTO;
import com.scrm.api.wx.cp.dto.RemainTimeDTO;
import com.scrm.api.wx.cp.entity.WxCustomer;
import com.scrm.api.wx.cp.entity.WxFissionContact;
import com.scrm.api.wx.cp.entity.WxFissionTask;
import com.scrm.api.wx.cp.vo.FissionInviteConditionVO;
import com.scrm.api.wx.cp.vo.WxFissionCustomerVO;
import com.scrm.api.wx.cp.vo.WxFissionTaskCustomerDetailH5VO;
import com.scrm.common.annotation.PassToken;
import com.scrm.common.constant.R;
import com.scrm.server.wx.cp.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * @author ：qiuzl
 * @date ：Created in 2022/3/25 11:25
 * @description：
 **/
@RestController
@RequestMapping("/wxFissionH5")
@Api(tags = {"企微应用宝-h5页面接口"})
public class WxFissionH5Controller {

    @Autowired
    private IWxFissionTaskService wxFissionTaskService;

    @Autowired
    private IWxFissionContactService wxFissionContactService;

    @Autowired
    private IWxFissionCustomerService fissionCustomerService;

    @Autowired
    private IWxFissionTaskCustomerDetailService fissionTaskCustomerDetailService;

    @Autowired
    private IWxCustomerService customerService;

    @Autowired
    private IWxTempFileService fileService;

    @PostMapping("/getPoster")
    @PassToken
    @ApiOperation(value = "获取个人海报")
    @Log(modelName = "企微应用宝-h5页面接口", operatorType = "获取个人海报")
    public R<String> getPoster(@Valid @RequestBody FissionPosterParamDTO dto) {
        return R.data(wxFissionContactService.getPoster(dto));
    }

    @GetMapping("/getTimeStamp")
    @PassToken
    @ApiOperation(value = "获取后台时间戳，记得要加上请求响应时间")
    @Log(modelName = "企微应用宝-h5页面接口", operatorType = "获取后台时间戳，记得要加上请求响应时间")
    public R<Long> getTimeStamp(){
        return R.data(System.currentTimeMillis());
    }

    @GetMapping("/getTaskInfo")
    @PassToken
    @ApiOperation(value = "获取任务剩余时间,判断是否已过期都靠它")
    @Log(modelName = "企微应用宝-h5页面接口", operatorType = "获取任务剩余时间,判断是否已过期都靠它")
    public R<RemainTimeDTO> getTaskInfo(@RequestParam String taskId){
        WxFissionTask wxFissionTask = wxFissionTaskService.checkExistsIncludeDel(taskId);

        return R.data(new RemainTimeDTO().setSystemTime(new Date()).setEndTime(wxFissionTask.getEndTime()));
    }

    @PostMapping("/getCustomerDetails")
    @PassToken
    @ApiOperation("获取助力客户详情")
    @Log(modelName = "企微应用宝-h5页面接口", operatorType = "获取助力客户详情")
    public R<IPage<WxFissionCustomerVO>> getCustomerDetails(@Valid @RequestBody FissionInviteConditionVO vo){
        if (StringUtils.isBlank(vo.getExtCorpId())) {
            throw new BaseException("企业id必填！");
        }
        return R.data(fissionCustomerService.getInviteDetails(vo));
    }

    @PostMapping("/getFinishDetails")
    @PassToken
    @ApiOperation("获取完成任务情况(分页参数随便传，不生效)")
    @Log(modelName = "企微应用宝-h5页面接口", operatorType = "获取完成任务情况(分页参数随便传，不生效)")
    public R<List<WxFissionTaskCustomerDetailH5VO>> getFinishDetails(@Valid @RequestBody FissionInviteConditionVO vo){
        if (StringUtils.isBlank(vo.getExtCorpId())) {
            throw new BaseException("企业id必填！");
        }
        return R.data(fissionTaskCustomerDetailService.getFinishDetails(vo));
    }

    @GetMapping("/getCustomerInfo")
    @PassToken
    @ApiOperation("获取客户信息")
    @Log(modelName = "企微应用宝-h5页面接口", operatorType = "获取客户信息")
    public R<WxCustomer> getCustomerInfo(@RequestParam String corpId, @RequestParam String extCustomerId){
        return R.data(customerService.checkExists(corpId, extCustomerId));
    }

    @GetMapping("/downloadQrCode")
    @PassToken
    @ApiOperation("下载任务的渠道活码")
    @Log(modelName = "企微应用宝-h5页面接口", operatorType = "下载任务的渠道活码")
    public void downloadQrCode(@RequestParam String corpId, @RequestParam String taskId, HttpServletResponse response){
        WxFissionContact contact = wxFissionContactService.getSysContact(corpId, taskId);
        fileService.downloadFile(contact.getQrCode(), response);
    }

    @GetMapping("/getQrCode")
    @PassToken
    @ApiOperation("获取任务的渠道活码")
    @Log(modelName = "企微应用宝-h5页面接口", operatorType = "获取任务的渠道活码")
    public R<String> getQrCode(@RequestParam String corpId, @RequestParam String taskId){
        return R.data(wxFissionContactService.getSysContact(corpId, taskId).getQrCode());
    }
}
